/**
 * FileName: TaskTimer
 * Author:   Leopold
 * Date:     2018/10/7 10:06
 * Description:
 * History:
 */
package cn.mcsys.platform.core.service;

import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.common.utils.MapUtils;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCarSumRecord;
import cn.mcsys.myadmin.modules.air.entity.AirRideQrCodeSpecial;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.entity.AirShuttlebusRecord;
import cn.mcsys.myadmin.modules.platform.entity.AirVehicleGpsBus;
import cn.mcsys.myadmin.modules.worker.entity.AirTaxi;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TaskTimer {

    private static List<Point2D.Double> core_area = new ArrayList<>();

    private static List<Point2D.Double> airshow_p4 = new ArrayList<>();

    public static final int SHUTTLEBUST_FINISH = 1;

    static {
        core_area.add(new Point2D.Double(113.412854, 22.076612));
        core_area.add(new Point2D.Double(113.421609, 22.065874));
        core_area.add(new Point2D.Double(113.399293, 22.022118));
        core_area.add(new Point2D.Double(113.364145, 21.996208));
        core_area.add(new Point2D.Double(113.345606, 22.051905));
        airshow_p4.add(new Point2D.Double(113.369786,22.006677));
        airshow_p4.add(new Point2D.Double(113.370816,22.007582));
        airshow_p4.add(new Point2D.Double(113.375363,22.001335));
        airshow_p4.add(new Point2D.Double(113.376361,22.00224));
    }

    @Autowired
    private BackService backService;

    /**
     * 查询核心区域内有多少出租车辆
     */
    public void StatisticalTaxSum() {
        System.out.println("计算核心区的所有出租车");
//        查询所有的出租车
        List<AirTaxi> airTaxis = backService.selectAllTaxi();
        int sum = 0;
        for (AirTaxi airTaxi : airTaxis) {
            if (airTaxi.getLon() == null || airTaxi.getLat() == null || "".equals(airTaxi.getLon()) || "".equals(airTaxi.getLat())) {
                continue;
            }
            Double lon = Double.valueOf(airTaxi.getLon());
            Double lat = Double.valueOf(airTaxi.getLat());
            if (MapUtils.IsPtInPoly(new Point2D.Double(lon, lat), core_area)) {
                sum += 1;
            }
        }
        AirCarSumRecord airCarSumRecord = new AirCarSumRecord();
        airCarSumRecord.setCarSum(sum);
        airCarSumRecord.setRecordDate(new Date());
        backService.saveTaxiSumRecord(airCarSumRecord);
    }

    /**
     * 计算穿梭巴士的趟数
     */
    public void checkShuttleBusTurns(){
//        查询出所有的穿梭巴士
//        List<AirVehicleGpsBus> shuttleBus = backService.selectByShuttleBus();
        List<AirVehicleGpsBus> shuttleBus = backService.getBusByNote("chuanshu");
        for (AirVehicleGpsBus bus : shuttleBus) {
//            判断是否在P4停车场中
            Double lon = Double.valueOf(bus.getLon());
            Double lat = Double.valueOf(bus.getLat());
            if(MapUtils.IsPtInPoly(new Point2D.Double(lon,lat),airshow_p4)){
//                记录在车辆状态表中,8状态为跑完一趟
                AirShuttlebusRecord airShuttlebusRecord = backService.selectShuttlebusRecord(bus.getId());
                Date currentDate = new Date();
                if (DateUtils.getDistanceMinutesOfTwoDate(airShuttlebusRecord.getRecordDate(),currentDate) > 5){
                    backService.insertBusRecord(bus,SHUTTLEBUST_FINISH);
                }
            }
        }
    }

    public void clearWaitingSum(){
        List<AirRidePoint> airRidePoints = backService.selectAllPoint2();
        for (AirRidePoint airRidePoint : airRidePoints) {
            backService.updateWaitingSum(airRidePoint.getId(),0);
        }
    }
//1,5,4,6,2,11,9,7,8
    public void updateSpecialCodeRidePoint(){
//        查询所有note为null的通用二维码
        List<AirRideQrCodeSpecial> airRideQrCodeSpecials = backService.selectByNotNull4SpecialCode();
        for (AirRideQrCodeSpecial airRideQrCodeSpecial : airRideQrCodeSpecials) {
            String machineNumber = airRideQrCodeSpecial.getMachineNumber();
            switch (machineNumber){
                case "10011":
                case "10012":airRideQrCodeSpecial.setNote("1");break;
                case "20002":
                case "20003":airRideQrCodeSpecial.setNote("5");break;
                case "30001":
                case "30002":airRideQrCodeSpecial.setNote("4");break;
                case "40001":
                case "40002":airRideQrCodeSpecial.setNote("6");break;
                case "50001":
                case "50003":airRideQrCodeSpecial.setNote("2");break;
                case "60001":
                case "60002":airRideQrCodeSpecial.setNote("11");break;
                case "70001":
                case "70002":airRideQrCodeSpecial.setNote("9");break;
                case "80003":
                case "80005":airRideQrCodeSpecial.setNote("7");break;
                case "90001":
                case "90002":airRideQrCodeSpecial.setNote("8");
            }
            backService.updateRideQrCodeSpecial(airRideQrCodeSpecial);
        }
    }
}
